﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.ProcessModel;
using System.Drawing;
using System.Threading;
using Hi_Baidu.Entry.CollectionObject;

namespace Hi_Baidu.Notify
{
    /// <summary>
    ///     提供了程序任务栏图标的相关管理操作
    /// </summary>
    class SystemNotifyIconManager : Manager
    {
        /// <summary>
        ///     任务栏提示计时控制
        /// </summary>
        private System.Timers.Timer NotifyTimer;
        /// <summary>
        ///     提示状态
        /// </summary>
        private bool NotifyState = false;
        private Icon ICON_NORMAL;
        private Icon ICON_TRANS;

        /// <summary>
        ///     开始提示任务栏
        /// </summary>
        public void Start()
        {
            //如果当前没有提示过任务栏图标
            if(!this.NotifyState)
            {
                this.NotifyTimer.Start();
                this.NotifyState = true;
            }
        }

        public SystemNotifyIconManager()
        {
            Globle.user.NI.DoubleClick += new EventHandler(NI_DoubleClick);
            this.ICON_NORMAL = new Icon("../Icon/Notify_Normal.ico", new Size(16, 16));
            ICON_TRANS = new Icon("../Icon/Trans.ico");
            this.NotifyTimer = new System.Timers.Timer(1000);
            NotifyTimer.Elapsed += new System.Timers.ElapsedEventHandler(NotifyTimer_Elapsed);
            if (this.Collection.Count > 0)
            {
                NotifyTimer.Start();
            }
        }

        void NI_DoubleClick(object sender, EventArgs e)
        {
            Notify();
        }

        void NotifyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Thread.Sleep(500);
            //当前可以提示任务栏，并且用户登录成功
            if (this.NotifyState && LoginProcessModel.LOGINSTATE == LoginProcessModel.LOGIN.LOGINED)
           {
                       switch((this.Collection[0] as Notify).NotifyType)
                       {
                           //申请好友类型
                           case NotifyType.NOTIFYTYPE.POST_ADD_FRIEND:
                               CALL_POST_ADD_FRIEND();
                               break;
                           //好友聊天信息类型
                           case NotifyType.NOTIFYTYPE.TALK_MESSAGE:
                               CALL_POST_GET_FRIEND_MESSAGE();
                               break;
                       } 
           }
            //当前用户没有登陆
            else if (this.NotifyState && LoginProcessModel.LOGIN.LOGINED == LoginProcessModel.LOGIN.LOGIN_AGO)
            {
                this.NotifyTimer.Stop();
                this.NotifyState = false;
            }
        }

        /// <summary>
        ///     申请好友动作调用
        /// </summary>
        private void CALL_POST_ADD_FRIEND()
        {
            Globle.user.NI.Text = "用户: " + (this.Collection[0] as Notify).GetUsername() + " 邀请您加入好友";
            Globle.user.NI.Icon = this.ICON_NORMAL;
            Thread.Sleep(1500);
            if (this.NotifyState)
            {
                Globle.user.NI.Icon = this.ICON_TRANS;
                Thread.Sleep(1500);
            }
        }

        /// <summary>
        ///     申请好友动作调用
        /// </summary>
        private void CALL_POST_GET_FRIEND_MESSAGE()
        {
            String FriendName = (this.Collection[0] as Notify).GetUsername();
            Globle.user.NI.Text = "好友: " + FriendName + " 的信息...";
            Globle.user.NI.Icon = Icon.FromHandle(new Bitmap("../syshead/" + (Globle.friendCollection.GetMemberByKey(FriendName) as Friend).HeadImageName + "_s.jpg").GetHicon());
            Thread.Sleep(1500);
            if (this.NotifyState)
            {
                Globle.user.NI.Icon = this.ICON_TRANS;
                Thread.Sleep(1500);
            }
        }

        /// <summary>
        ///     获得下一个要提示的对象
        /// </summary>
        /// <returns>返回下一个要提示的对象</returns>
        public Notify GetNextNotify()
        {
            if (this.Collection.Count > 0)
            {
                this.Collection.RemoveAt(0);
            }
            if (this.Collection.Count > 0)
            {
                return this.Collection[0] as Notify;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        ///     检测当前任务栏提示状态
        /// </summary>
        /// <returns>返回false, 则表示不可提示</returns>
        public bool CheckNotifyState()
        {
            return NotifyState;
        }

        /// <summary>
        ///     开始下一个任务栏提示
        /// </summary>
        public void Notify()
        {
            this.NotifyState = false;
            this.NotifyTimer.Stop();
            //启动队列第一个对象的触发器
            if ((this.Collection[0] as Notify) != null)
            {
                (this.Collection[0] as Notify).Trigger();
                Hi_Baidu.Notify.Notify notify = this.GetNextNotify();
                if (notify == null)
                {
                    //用户已经登录
                    if (LoginProcessModel.LOGINSTATE == LoginProcessModel.LOGIN.LOGINED)
                    {
                        Globle.user.NI.Icon = new Icon(@"../Icon/ICON_LL/460.ico", new System.Drawing.Size(16, 16));
                        LoginProcessModel.TOOLTIP_NOTIFY_ICON();
                    }
                    //用户没有登录
                    else if (LoginProcessModel.LOGIN.LOGINED == LoginProcessModel.LOGIN.LOGIN_AGO)
                    {
                        cls_Application_Init.INIT_NOTIFY_ICON();
                    }
                }
                else
                {
                    this.NotifyTimer.Start();
                    this.NotifyState = true; ;
                }
            }
        }
    }
}
